package com.koc.robot.freetype;

import com.koc.robot.util.BufferUtils;
import java.nio.ByteBuffer;

public class FreeType 
{
	public static int FT_LOAD_DEFAULT				= 0x0;
	
	public static int FT_RENDER_MODE_NORMAL = 0;
	
	/*
	 * JNI for freetype library, we only expose the FreeAPI that this game needs
	 * */
	
	public static Library initFreeType()
	{
		System.loadLibrary("FreeType2");
		long address = initFreeTypeJni();
		if (address == 0) throw new NullPointerException();
		else return new Library(address);
	}
	
	private static native long initFreeTypeJni();
	
	public static void doneFreeType(Library library)
	{
		doneFreeTypeJni(library.address);
	}
	
	private static native void doneFreeTypeJni(long library);
	
	
	public static Face newMemoryFace(Library library, byte[] data, int dataSize, int faceIndex)
	{
		ByteBuffer buffer = BufferUtils.newUnsafeByteBuffer(data.length);
		BufferUtils.copy(data, 0, buffer, data.length);
		long address = newMemoryFace(library.address, buffer, dataSize, faceIndex);
		if (address == 0)
		{
			BufferUtils.disposeUnsafeByteBuffer(buffer);
			throw new RuntimeException("Couldn't load font");
		}
		else
		{
			return new Face(address, library);
		}
	}
	
	private static native long newMemoryFace(long library, ByteBuffer data, int dataSize, int faceIndex);
	
	public static void doneFace(Face face)
	{
		doneFace(face.address);
	}
	
	private static native void doneFace(long face);
	
	public static boolean loadGlyph(Face face, int glyphIndex, int loadFlags)
	{
		return loadGlyph(face.address, glyphIndex, loadFlags);
	}
	
	public static boolean setPixelSizes(Face face, int pixelWidth, int pixelHeight)
	{
		return setPixelSizes(face.address, pixelWidth, pixelHeight);
	}
	
	private static native boolean setPixelSizes(long face, int pixelWidth, int pixelHeight);
	
	private static native boolean loadGlyph(long face, int glyphIndex, int loadFlags);
	
	public static int getCharIndex(Face face, int charCode)
	{
		return getCharIndex(face.address, charCode);
	}
	
	private static native int getCharIndex(long face, int charCode);
	
	public static boolean renderGlyph(GlyphSlot slot, int renderMode)
	{
		return renderGlyph(slot.address, renderMode);
	}
	
	private static native boolean renderGlyph(long slot, int renderMode);
}
